package com.cloudcc.mobile.view.web;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.view.base.BaseWebViewFragment;

/**
 * @Description: web页面 参数里面必须有 url
 * @author wangzhiqiang
 * @date 2015-12-4 上午11:18:41
 * 
 */
public class WebFragment extends BaseWebViewFragment {

	protected static final int STEP_START = 1;
	protected static final int STEP_OVERRIDEURL = 2;
	protected static final int STEP_FINISH = 3;

	private ISyncInterface mISyncInterface;

	@Override
	public void init() {
		super.init();
		initSync();
	}
	
	private void initSync() {
		Bundle bundle = getArguments();
		if (bundle==null) {
			return;
		}
		mISyncInterface =(ISyncInterface) bundle.getSerializable("callback");
	}

	@Override
	protected void onPreInitWebView() {
		Bundle bundle = getArguments();
		if (bundle != null) {
			mUrl = bundle.getString(ExtraConstant.EXTRA_URL, "");
		}
	}
//
//	protected void initSyncUrl() {
//		mSyncUrls = new ArrayList<SyncParam>();
//		mSyncUrls.add(new SyncParam("m=save", true, STEP_FINISH,new String[]{"m=query","WeiXin_index.action"}));
//		mSyncUrls.add(new SyncParam("wx_taskquery.action?m=query", true, STEP_OVERRIDEURL,new String[]{"m=list"}));
//		mSyncUrls.add(new SyncParam("m=delete", true, STEP_FINISH,new String[]{""}));
//		mSyncUrls.add(new SyncParam("/WeiXin_reassignWorkitem.action", true, STEP_FINISH,new String[]{"WeiXin_index.action"}));
//		mSyncUrls.add(new SyncParam("/WeiXin_processApproval.action", true, STEP_FINISH,new String[]{"WeiXin_index.action","weixin_index.action"}));
//	}

	@Override
	public WebViewClient getWebViewClient() {
		return new SyncWebClient();
	}

	private class SyncWebClient extends BaseWebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			if (mISyncInterface!=null) {
				mISyncInterface.onPageStarted(view, url);
			}
			
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			if (mISyncInterface!=null) {
				mISyncInterface.onPageFinished(view, url);
			}
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			LogUtils.d("webview", "跳转url:" + url);
			if (mISyncInterface!=null) {
				return mISyncInterface.shouldOverrideUrlLoading(view, url);
			}else{
				return super.shouldOverrideUrlLoading(view, url);
			}
		}
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}
